package com.xxl.job.admin.utils;

import com.xxl.job.core.commons.exception.DataxException;
import com.xxl.job.core.commons.response.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Set;

/**
 * @autor xuyue
 * @email xuyue@cetcbigdata.com
 * @date 2021-4-21 8:51
 */
@Slf4j
public class ValidatorUtils {
    private static Validator validator;

    static {
        MessageSourceResourceBundleLocator messageSourceResourceBundleLocator = new MessageSourceResourceBundleLocator(getMessageSource());
        ResourceBundleMessageInterpolator resourceBundleMessageInterpolator = new ResourceBundleMessageInterpolator(messageSourceResourceBundleLocator);
        validator = Validation
                .byDefaultProvider()
                .configure()
                .messageInterpolator(resourceBundleMessageInterpolator)
                .buildValidatorFactory().getValidator();
    }

    private static ResourceBundleMessageSource getMessageSource() {
        ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource();
        bundleMessageSource.setDefaultEncoding("UTF-8");
        bundleMessageSource.setBasenames("i18n/validation", "i18n/validation_common");
        return bundleMessageSource;
    }

    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @param groups 待校验的组
     */
    public static void validateEntity(Object object, Class<?>... groups)
            throws DataxException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            StringBuilder msg = new StringBuilder();
            for (ConstraintViolation<Object> constraint : constraintViolations) {
                // 每个错误后添加分号
                String message = constraint.getMessage();
                message.replaceAll(" ", "");
                msg.append(message).append(";");
            }
            // 定义一个中文排序器
            String[] sortMsg = msg.toString().split(";");
            Comparator comparator = Collator.getInstance(Locale.CHINA);
            Arrays.sort(sortMsg, comparator);
            StringBuilder errorMsg = new StringBuilder();
            for (String item : sortMsg) {
                errorMsg.append(item).append(";");
            }
            // 处理并返回信息
            String retErrorMsg = errorMsg.toString();
            retErrorMsg = retErrorMsg.substring(0, retErrorMsg.lastIndexOf(";"));
            log.info("校验不通过 -- " + retErrorMsg);
            DataxException.cast(retErrorMsg, ReturnCode.INVALID_PARAMETER);
        }
    }


}
